package wangmin.message.mgr_web.web.service;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by wangmin on 2019/1/21.
 */
@Service
public class AuthService {
    private static final String hasLoginFlagName = "hasLoginFlag";
    @Value("${need.auth}")
    private boolean needAuth;
    @Value("${manager.name}")
    private String userName;
    @Value("${manager.password}")
    private String userPassword;

    public boolean userLogin(String userName, String userPassword, HttpServletRequest request) {
        if (hasLogin(request))
            return true;

        if (!needAuth || (this.userName.equals(userName) && this.userPassword.equals(userPassword))) {
            request.getSession().setAttribute(hasLoginFlagName, true);
            return true;
        }

        return false;
    }

    public boolean hasLogin(HttpServletRequest request) {
        return (!needAuth) || (null != request.getSession().getAttribute(hasLoginFlagName));
    }
}
